package yourauthor.utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author rosstizma
 */
public class ExclusionWordFilter {
    
    private static Set<String> exclusions = new HashSet<String>();
    static {
        exclusions.add("");
    }
    
    public static void addExclusionsFromFile(InputStream exclusionsFile) {
        String exclusionsFileAsString = "";
        try {
            int read = 0;
            byte[] b = new byte[1024];
            while ( (read = exclusionsFile.read(b)) != -1 ) {
                byte[] bytesToAdd = Arrays.copyOfRange(b, 0, read);
                exclusionsFileAsString += new String(bytesToAdd);
            }
        } catch (IOException ex) {
            System.out.println("WARNING: An error has occured reading the exclusions file");
        } finally {
            try {
                exclusionsFile.close();
            } catch (IOException ex) {
                System.out.println("WARNING: An error has occured reading the exclusions file");
            }
        }
        String[] wordsToExclude = exclusionsFileAsString.split("[\n,]");
        for ( String wordToExclude : wordsToExclude ) {
            exclusions.add(wordToExclude);
        }
    }
    
    public static boolean checkAgainstExclusionList(String stringToCompare) {
        for ( String exclusion : exclusions ) {
            if ( stringToCompare.equals(exclusion) )
                return true;
        }
        return false;
    }
    
}
